
package com.ds.msg.mqtt.command.filter.appmsg;

import com.ds.common.ConfigCode;
import com.ds.common.JDSException;
import com.ds.engine.JDSSessionHandle;
import com.ds.msg.Msg;
import com.ds.msg.MsgType;
import com.ds.msg.filter.MsgFilter;
import com.ds.msg.mqtt.client.JMQClient;
import com.ds.server.JDSServer;

/**
 * @author wenzhang
 */
public class SMSDataFilterImpl implements MsgFilter {


    private JDSSessionHandle handle;


    public boolean filterObject(Msg msg, JDSSessionHandle handle) {

        MsgType type = MsgType.fromType(msg.getType());
        this.handle = handle;

        if (type.equals(MsgType.MSG)) {
            try {
                sendMessage(msg);
            } catch (JDSException e) {
                e.printStackTrace();
            }
            return true;
        }

        return false;

    }


    private void sendMessage(Msg msg) throws JDSException {

        String systemCode = JDSServer.getInstance().getSessionhandleSystemCodeCache().get(handle.toString());
        if (systemCode != null) {
            JMQClient client = (JMQClient) JDSServer.getInstance().getJDSClientService(handle, ConfigCode.fromType(systemCode));
            if (client != null && client.getConnectInfo() != null) {
                client.sendSystemMsg(msg);
            }
        }

    }


}
